/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import fever.app.context.abstract.FeverContext;
import fever.app.context.abstract.IDialog;
import fever.app.context.abstract.IFileSystem;
import fever.app.context.ContextManager;
import fever.core.client.ClientContext;
import fever.core.CoreApplication;
import fever.display.FeverStage;
import fever.exception.Exception;
import fever.FeverApplication;
import fever.log.FeverDebug;

/**
 * Main access point to Fever Framework.
 * 
 * <p>Use it as application starting point.
 *
 * <p>Example
 * {@code
 *   public static function main() : Void
 *   {
 *   	Fever.run( new MainTest() );
 *   }
 * }  
 * 
 * @see fever.FeverApplication
 * @see fever.core.CoreApplication * @see fever.core.client.ClientContext
 * @see fever.display.FeverStage
 * 
 * @author Romain Ecarnot
 */
class fever.Fever
{		
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private static var _instance : Fever; 
	private static var _thread : CoreApplication;	private static var _client : ClientContext;	private static var _stage : FeverStage;
	
	
	//-------------------------------------------------------------------------
	// Public Properties
	//-------------------------------------------------------------------------
	
	/** Application configuration file. */
	public static var configURL : String = 'config.xml';
	
	/** Returns running {@link fever.core.Application} instance. */ 
	public static function get application() : CoreApplication { return _thread; }
		
	/** Returns client object. */
	public static function get client() : ClientContext { return _client; }
	
	/** Returns application filesystem. */
	public static function get io() : IFileSystem { return ContextManager.getInstance().io; }
	
	/** Returns application dialog system. */
	public static function get dialog() : IDialog { return ContextManager.getInstance().dialog; }
	
	/** Returns default {@link FeverStage} object */
	public static function get stage() : FeverStage
	{ 
		if (!_stage ) _stage = FeverStage.getInstance();
		return _stage;
	}
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Main Fever Framework access.
	 * 
	 * <p>Currently used in you {@code main} method to inititalize your app.
	 * 
	 * @param app {@link fever.FeverApplication} instance	 * @param context ( optional ){@link fever.app.context.abstract.FeverContext} instance
	 * @param config (optional) {@link fever.conf.FeverConfiguration} instance
	 */
	public static function run( app : FeverApplication, context : FeverContext, enableConfig : Boolean ) : Void
	{
		if( !_instance ) _instance = new Fever( app, context, enableConfig );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	private function Fever( app : FeverApplication, context : FeverContext, enableConfig : Boolean )
	{
		Exception.isOn = FeverDebug.isOn;
		
		ContextManager.getInstance( context );
		
		_client = ClientContext.getInstance();
		_stage = FeverStage.getInstance();
		_thread = CoreApplication.getInstance( app, enableConfig );
		
		_stage.init();
		_thread.init();
	}
}